package com.jgsu.sell.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jgsu.sell.dataobject.OrderDetail;
import com.jgsu.sell.dto.OrderDto;
import com.jgsu.sell.enums.ResultEnum;
import com.jgsu.sell.exception.SellException;
import com.jgsu.sell.from.OrderForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * orderform转orderDto
 *
 * @author lqd12
 * @create 2018-08-19 23:31
 */
@Slf4j
public class OrderForm2OrderDtoConverter {


    public static OrderDto converter(OrderForm orderForm){
        Gson gson = new Gson();
        OrderDto orderDto = new OrderDto();
        orderDto.setBuyerName(orderForm.getName());
        orderDto.setBuyerPhone(orderForm.getPhone());
        orderDto.setBuyerOpenid(orderForm.getOpenid());
        orderDto.setBuyerAddress(orderForm.getAddress());
        List<OrderDetail> orderDetailList = new ArrayList<>();
        try {
            orderDetailList = gson.fromJson(orderForm.getItems()
                    ,new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            log.error("【对象转换】，错误，String = {}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        orderDto.setOrderDetailList(orderDetailList);
        return orderDto;
    }
}